<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="医院名称">
        <el-input v-model="hospset.hosname" />
      </el-form-item>
      <el-form-item label="医院编号">
        <el-input v-model="hospset.hoscode" />
      </el-form-item>
      <el-form-item label="api地址">
        <el-input v-model="hospset.apiUrl" />
      </el-form-item>
      <el-form-item label="联系人">
        <el-input v-model="hospset.contactsName" />
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="hospset.contactsPhone" />
      </el-form-item>
      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import hospset from '@/api/hospitalset.js'
export default {
  data() {
    return {
      hospset: {},
      saveBtnDisabled: false // 保存按钮是否禁用,
    }
  },
  methods: {
    saveOrUpdate() {
      this.saveBtnDisabled = true
      if (!this.hospset.id) {
        // 操作新增
        this.saveData()
      } else {
        // 操作修改
        this.updateData()
      }
    },
    // 保存
    saveData() {
      hospset.save(this.hospset).then(response => {
        return this.$message({
          type: 'success',
          message: '保存成功'
        })
      }).then(response => {
        // 路由跳转
        this.$router.push({ path: '/hospitalset/list' })
      }).catch(response => {
        this.$message({
          type: 'info',
          message: '保存失败'
        })
      })
    },
    updateData() {
      hospset.updateData(this.hospset).then(response => {
        return this.$message({
          type: 'success',
          message: '修改成功'
        })
      }).then(response => {
        // 路由跳转
        this.$router.push({ path: '/hospitalset/list' })
      }).catch(response => {
        this.$message({
          type: 'error',
          message: '修改失败'
        })
      })
    }
  },
  // eslint-disable-next-line vue/order-in-components
  created() {
    // 操作数据回显 用于编辑 判断是否有传入参数
    if (this.$route.params && this.$route.params.id) {
      var id = this.$route.params.id
      hospset.datail(id).then(response => {
        this.hospset = response.data.item
      })
    }
  }
}
</script>
